/**
 * Output is a subclass of the class Put.
 * 
 * @author Gregory Banfi (gregory.banfi@usi.ch), @author Simone d'Avico (simone.davico@usi.ch); Giuseppe Mendola (giuseppe.mendola@usi.ch);
 * @version 17.05.2011
 */

package ch.usi.inf.pf2.puts;

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;

import ch.usi.inf.pf2.Value;
import ch.usi.inf.pf2.pins.InputPin;

public class Output extends Put implements Serializable {

	// Fields for the GUI
	private int left;
	private int top;
	private int width;
	private int height;
	private InputPin inputPin;
	private int time;

	/**
	 * Constructor of Input. It assigns a value to the value field of the put
	 * object.
	 * 
	 * @param value
	 *            A value object.
	 */
	public Output(final Value value, final int x, final int y) {
		super(value, x, y);
		this.time = 0;
		this.inputPin = new InputPin(Value.UNKNOWN);
		this.left = x;
		this.top = y;
		width = 20;
		height = 20;
	}

	/**
	 * Methods for the GUI
	 */

	/**
	 * This method draws an input with the defined dimension.
	 */
	@Override
	public void draw(Graphics g, Color color) {
		String v = "X";
		if (getValue() == Value.TRUE) {
			g.setColor(Color.GREEN);
			v = "1";
		} else if (getValue() == Value.FALSE) {
			g.setColor(Color.RED);
			v = "0";
		} else if (getValue() == Value.UNKNOWN) {
			g.setColor(color);
			v = "X";
		}
		g.drawOval(left, top, width, height);
		g.drawString(v, left + 7, top + 15);
		g.drawLine(left - 3, top + height / 2, left, top + height / 2);
	}

	/**
	 * Override methods form component.
	 */
	@Override
	// This method checks if there is an output where the user clicks the mouse.
	public boolean contains(int x, int y) {
		return x >= left && x <= left + width && y >= top && y <= top + height;
	}

	@Override
	// This method returns the left field.
	public int getOriginX() {
		return left;
	}

	@Override
	// This method returns the top field.
	public int getOriginY() {
		// TODO Auto-generated method stub
		return top;
	}

	@Override
	public void setOrigin(int x, int y) {
		this.left = x;
		this.top = y;
	}

	@Override
	public InputPin getInputPin() {
		return inputPin;
	}

	@Override
	public void setOutputValue() {
		setValue(inputPin.getValue());
	}


}
